package com.sojson.util.pay;

import com.sojson.base.impl.BaseOrder;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.config.exception.TransException;
import com.sojson.enums.core.EPayType;
import com.sojson.result.bean.Status;
import com.sojson.util.pay.ali.AliUtil;
import com.sojson.util.pay.wx.WxPayUtil;

/**
 * 支付工具类
 * 
 * @author liu
 * @date 2021-06-23
 */
public class PayUtil {

    /**
     * 退款(调这个方法前请先调用{@link BaseOrder#refundInit(String, java.math.BigDecimal, String, java.math.BigDecimal, Integer)})
     * 
     * @param order
     * @param nofityUrl 回调地址
     * @throws Exception 
     */
    public static Status refund(BaseOrder order, String nofityUrl) throws Exception {
        Status status = null;

        Integer payType = order.getBasePayType();
        if (EPayType.WX.getCode() == payType) {
            status = WxPayUtil.refund(order, nofityUrl);
        } else if (EPayType.ZFB.getCode() == payType) {
            status = AliUtil.refund(order);
        } else {
            throw new TransException("无此支付类型！");
        }

        if (status.getCode() != TransErrorCode.SUCCESS.getCode()) {
            throw new TransException(status.getMsg());
        }

        return status;
    }

}